<template>
  <div class="top-category">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread separator="/">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem>{{cateName}}</XtxBreadItem>
      </XtxBread>

      <!-- 轮播图 -->
      <XtxSlider :data="slidersData"/>

      <!-- 全部分类 -->
      <div class="sub-list">
        <h3>全部分类</h3>
        <ul>
          <li v-for="item in categoryList" :key="item.id">
            <a href="javascript:;">
              <img :src="item.picture" >
              <p>{{item.name}}</p>
            </a>
          </li>
        </ul>
      </div>

      <!-- 分类商品 -->
    <div class="ref-goods" v-for="item in categoryList" :key="item.id">
      <!-- 标题 -->
      <div class="head">
        <h3>-  {{item.name}}  -</h3>
        <XtxMore to="/" />
      </div>
      <!-- 商品 -->
      <div class="body">
      <!-- <RouterLink to="/" class="goods-item"  > -->
        <!-- <img
          :src="good.picture"
          alt=""
        />
        <p class="name ellipsis">{{good.name}}</p>
        <p class="desc ellipsis">{{good.desc}}</p>
        <p class="price">&yen;{{good.price}}</p> -->
      <!-- </RouterLink> -->
        <!-- 单个商品组件 -->
        <SecondGood v-for="good in item.goods" :key="good.id" :good="good"/>

      </div>
    </div>
    </div>
  </div>
</template>
<script>
import { findAllCategory } from '@/api/category'
import { useBanner } from '@/hook/page'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import SecondGood from './components/second-good.vue'
// 获取目录数据
const useCategory = () => {
  const categoryList = ref([])
  // 获取路由信息对象
  const route = useRoute()
  // 面包屑标题
  const cateName = ref('')
  // 调用api
  const getCategoryList = async (id) => {
    const res = await findAllCategory(id)
    // console.log(res)
    categoryList.value = res.result.children
    cateName.value = res.result.name
  }

  onMounted(() => {
    getCategoryList(route.params.id)
  })

  // 暴露数据
  return { categoryList, cateName }
}
export default {
  name: 'TopCategory',
  components: {
    SecondGood
  },
  setup () {
    // 获取轮播图数据
    const { slidersData } = useBanner()
    // 获取二级目录数据
    const { categoryList, cateName } = useCategory()
    return { slidersData, categoryList, cateName }
  }
}
</script>

<style scoped lang="less">
.top-category {
  h3 {
    font-size: 28px;
    color: #666;
    font-weight: normal;
    text-align: center;
    line-height: 100px;
  }
  .sub-list {
    margin-top: 20px;
    background-color: #fff;
    ul {
      display: flex;
      padding: 0 32px;
      flex-wrap: wrap;
      li {
        width: 168px;
        height: 160px;
        a {
          text-align: center;
          display: block;
          font-size: 16px;
          img {
            width: 100px;
            height: 100px;
          }
          p {
            line-height: 40px;
          }
          &:hover {
            color: @xtxColor;
          }
        }
      }
    }
  }
  .ref-goods {
    background-color: #fff;
    margin-top: 20px;
    position: relative;
    .head {
      .xtx-more {
        position: absolute;
        top: 20px;
        right: 20px;
      }
      .tag {
        text-align: center;
        color: #999;
        font-size: 20px;
        position: relative;
        top: -20px;
      }
    }
    .body {
      display: flex;
      justify-content: space-around;
      padding: 0 40px 30px;
    }
  }
}
</style>
